﻿using AdverseEventReportSystem.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AdverseEventReportSystem.BLL.SchedulingSystem;

namespace AdverseEventReportSystem.BLL
{
    /// <summary>
    /// 排班管理
    /// </summary>
    public class SchedulingManager : BaseManager
    {
        /// <summary>
        /// 保存排班
        /// </summary>
        /// <param name="schedulingList"></param>
        /// <param name="schedulingOvertimeList"></param>
        /// <param name="departmentCode"></param>
        /// <returns></returns>
        public static OperateResult SaveScheduling(List<AdverseEventReportSystem.BLL.SchedulingSystem.Scheduling> schedulingList, List<SchedulingOvertime> schedulingOvertimeList, string departmentCode)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SysLoginUser loginUser = LoginManager.GetLoginUser();
                string departmentName = loginUser.DepartName.Split('&')[Array.IndexOf(loginUser.DepartCode.Split('&'), departmentCode)];

                schedulingList = schedulingList.Select(x => { x.DepartmentCode = departmentCode; x.DepartmentName = departmentName; x.CreateUserID = loginUser.Personnel_id; return x; }).ToList();
                schedulingOvertimeList = schedulingOvertimeList.Select(x => { x.DepartmentCode = departmentCode; x.CreateUserID = loginUser.Personnel_id; return x; }).ToList();
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfBoolean result = service.SaveScheduling(schedulingList.ToArray(), schedulingOvertimeList.ToArray());
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="template"></param>
        /// <param name="templateInfo"></param>
        /// <param name="departmentCode"></param>
        /// <returns></returns>
        public static OperateResult SaveTemplate(SchedulingTemplate template, List<SchedulingTemplateInfo> templateInfo, string departmentCode)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SysLoginUser loginUser = LoginManager.GetLoginUser();
                template.DepartmentCode = departmentCode;
                template.DepartmentName = loginUser.DepartName.Split('&')[Array.IndexOf(loginUser.DepartCode.Split('&'), departmentCode)]; ;
                template.CreateUserID = loginUser.Personnel_id;

                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfBoolean result = service.SaveTemplate(template, templateInfo.ToArray());
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }


        /// <summary>
        /// 删除排班
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static OperateResult DelScheduling(DateTime startDate, DateTime endDate, string departmentCode)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfBoolean result = service.DelScheduling(startDate, endDate, departmentCode);
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult DelTemplate(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfBoolean result = service.DelTemplate(id);
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
